#version 460

#include "project_matrix.glsl"
#include "single_color.glsl"



//上排数量 上排多插座开始索引    下排数量 下排多插座开始索引
layout(std430, column_major, binding = 4) readonly buffer U4_Matrix {
	ivec4 m_SocketInI[];
};



layout(points) in;
layout(points, max_vertices=32) out;



in gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
} gl_in[];

layout(location = 1) in VS_GS_VERTEX {
	uint InstanceIndex;
}vertex_in[];





layout(location = 0) out vec4 gColor;
out gl_PerVertex {
	vec4	gl_Position;
	float	gl_PointSize;
};

//vec2 pos[4] = {vec2(10.0, 10.0), vec2(100.0, 10.0), vec2(10.0, 100.0), vec2(100.0, 100.0)};

void main(void){
	mat3x2 mat = m_LP_SD[vertex_in[0].InstanceIndex];
	//mat3x2 mat = m_LP_SD[gl_InvocationID];
	ivec4 dim = m_SocketInI[vertex_in[0].InstanceIndex];
	//ivec4 dim = ivec4(int(tempDim.x), int(tempDim.y), int(tempDim.z), int(tempDim.w));

	vec4 TColor = f_buildColor(InstanceColor[vertex_in[0].InstanceIndex]);
	
	//dim.x(输入节点数量)   dim.z(输出节点数量)
	
	//float s = (mat[1].x) / (dim.x);
	//float l = ((mat[1].x+mat[2].x) - (dim.x*s-s)) * 0.5;

	float s_step = mat[1].x / dim.x;
	//float l = ((mat[1].x - mat[2].x*2) - (dim.x*s_step-s_step)) * 0.5;
	for(int i=0; i<dim.x; ++i){
		gl_PointSize = m_Scale.x*8;
		vec4 v = vec4(0.0, 0.0, 0.0, 1.0);

		if(bool(i)){
			v.xy += mat[0] + vec2(i*s_step, -5);
			
		}else{
			v.xy += mat[0] + vec2(4, 4);
		}

		v.xy *= m_Scale;
		v.xy += m_Loc;
		v.z = mat[2].y;
		//v.xy += mat[0] + vec2(i*s + l, -5);
		//v.xy += mat[0] + vec2(i*s_step + mat[2].x + 4, -5);
		gl_Position = mat_ProjecView * v;
		
		gColor = TColor;

		EmitVertex();
	}

	//s = (mat[1].x) / (dim.z);
	s_step = mat[1].x / dim.z;
	//l = ((mat[1].x+mat[2].x) - (dim.z*s-s)) * 0.5;
	//l = ((mat[1].x - mat[2].x*2) - (dim.z*s-s)) * 0.5;
	for(int i=0; i<dim.z; ++i){
		gl_PointSize = m_Scale.x*8;
		vec4 v = vec4(0.0, 0.0, 0.0, 1.0);


		if(bool(i)){
			v.xy += mat[0] + vec2(i*s_step, (mat[1].y+mat[2].x) );
		}else{
			v.xy += mat[0] + vec2(4, (mat[1].y - 4));
		}
		//v.xy += mat[0] + vec2(i*s + l, (mat[1].y+mat[2].x) + 5 );
		//
		v.xy *= m_Scale;
		v.xy += m_Loc;

		v.z = mat[2].y;

		gl_Position = mat_ProjecView * v;
		

		gColor = TColor;

		EmitVertex();
	}
	
	EndPrimitive();
}



 